'''
QTextEdit控件输入多行文本
'''

from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import *
import sys


class QTextEditDemo(QWidget):
    def __init__(self):
        super(QTextEditDemo, self).__init__()
        self.iniUI()

    def iniUI(self):
        self.setWindowTitle('QTextEdit控件演示')
        self.resize(400, 300)
        #  textEdit为实例属性
        self.textEdit = QTextEdit()
        self.textEdit.setFont(QFont('微软雅黑',40))
        self.buttonText = QPushButton('显示文本')
        self.buttonText.setFont(QFont('Microsoft JhengHei', 10))
        self.buttonHTML = QPushButton('显示HTML')
        self.buttonHTML.setFont(QFont('Microsoft JhengHei', 10))
        self.buttonToText = QPushButton('获取文本')
        self.buttonToHTML = QPushButton('获取HTML')

        layout = QVBoxLayout()
        layout.addWidget(self.textEdit)
        layout.addWidget(self.buttonText)
        layout.addWidget(self.buttonHTML)
        layout.addWidget(self.buttonToText)
        layout.addWidget(self.buttonToHTML)

        self.setLayout(layout)

        #  设置文本按钮绑定槽函数
        self.buttonText.clicked.connect(self.onClick_ButtonText)
        #  设置HTML绑定槽函数
        self.buttonHTML.clicked.connect(self.onClick_ButtonHTML)
        self.buttonToText.clicked.connect(self.onClick_ButtonToText)
        self.buttonToHTML.clicked.connect(self.onClick_ButtonToHTML)

    def onClick_ButtonText(self):
        self.textEdit.setPlainText('Hello World,世界你好吗')

    def onClick_ButtonToText(self):
        print(self.textEdit.toPlainText())

    def onClick_ButtonHTML(self):
        self.textEdit.setHtml('<font color="blue">Hello World</font>')

    def onClick_ButtonToHTML(self):
        print(self.textEdit.toHtml())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QTextEditDemo()
    main.show()
    sys.exit(app.exec_())